package cn.hahah.module.blog.app.controller;

import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.module.blog.app.JwtUtils;
import cn.hahah.module.blog.app.service.BlogUserService;
import cn.hahah.module.blog.controller.admin.article.vo.ArticlePageReqVO;
import cn.hahah.module.blog.controller.admin.article.vo.ArticleRespVO;
import cn.hahah.module.blog.controller.admin.article.vo.ArticleSaveReqVO;
import cn.hahah.module.blog.controller.admin.articletag.vo.ArticleTagSaveReqVO;
import cn.hahah.module.blog.controller.admin.usercollect.vo.UserCollectSaveReqVO;
import cn.hahah.module.blog.controller.admin.usercomment.vo.UserCommentSaveReqVO;
import cn.hahah.module.blog.controller.admin.userfocus.vo.UserFocusSaveReqVO;
import cn.hahah.module.blog.controller.admin.userfriend.vo.UserFriendSaveReqVO;
import cn.hahah.module.blog.controller.admin.userreply.vo.UserReplySaveReqVO;
import cn.hahah.module.blog.controller.admin.users.vo.UsersPageReqVO;
import cn.hahah.module.blog.controller.admin.users.vo.UsersRespVO;
import cn.hahah.module.blog.controller.admin.users.vo.UsersSaveReqVO;
import cn.hahah.module.blog.dal.dataobject.article.ArticleDO;
import cn.hahah.module.blog.dal.dataobject.usercomment.UserCommentDO;
import cn.hahah.module.blog.dal.dataobject.userdialogue.UserDialogueDO;
import cn.hahah.module.blog.dal.dataobject.userhistorybrowse.UserHistoryBrowseDO;
import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.article.ArticleService;
import cn.hahah.module.blog.service.tag.TagService;
import cn.hahah.module.blog.service.users.UsersService;
import cn.hutool.log.Log;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


@RestController
@Tag(name = "博客访问模块_用户接口")
@RequestMapping("/public/user")
public class BlogUserController {

    @Resource
    private BlogUserService blogUserService;
    @Resource
    private JwtUtils jwtUtils;
    @Resource
    private ArticleService articleService;
    @Resource
    private TagService tagService;
    @Resource
    private UsersService usersService;

    /**用户_查看个人信息*/
    @GetMapping("/userGet")
    @Operation(summary = "用户_查看个人信息")
    public CommonResult<UsersDO> userGet(
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        UsersDO usersDO =  usersService.getUsers(userId).setPassword(null);
        return CommonResult.success(usersDO);
    }

    /**用户_修改个人信息*/
    @PostMapping("/userSet")
    @Operation(summary = "用户_修改个人信息")
    public CommonResult<UsersRespVO> userSet(
            @RequestBody UsersPageReqVO usersPageReqVO,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.userSet(userId, usersPageReqVO);
    }

    /**用户_发布文章*/
    @PostMapping("/article/create")
    @Operation(summary = "用户_发布文章")
    public CommonResult<String> userCreateArt(
            @RequestBody ArticleSaveReqVO articleSaveReqVO,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        articleSaveReqVO.setUserId(userId);
        articleSaveReqVO.setDescription(articleSaveReqVO.getTitle());
        return blogUserService.userCreateArt(articleSaveReqVO);
    }

    /**用户_编辑文章*/
    @PostMapping("/userUpdateArt")
    @Operation(summary = "用户_编辑文章")
    public CommonResult<String> userUpdateArt(
            @RequestBody ArticleSaveReqVO articleSaveReqVO,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        articleSaveReqVO.setUserId(userId);
        articleSaveReqVO.setDescription(articleSaveReqVO.getTitle());
        return blogUserService.userUpdateArt(articleSaveReqVO);
    }

    /**用户_查看个人文章*/
    @PostMapping("/getUserArtPage")
    @Operation(summary = "用户_查看个人文章")
    public CommonResult<PageResult<ArticleRespVO>> getUserArtPage(
            @RequestBody ArticlePageReqVO articlePageReqVO,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        articlePageReqVO.setUserId(userId);
        return blogUserService.getUserArtPage(articlePageReqVO);
    }

    /**用户_删除个人文章*/
    @GetMapping("/delUserArtPage")
    @Operation(summary = "用户_删除个人文章")
    public CommonResult<String> delUserArtPage(
            @RequestParam Long articleId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.delUserArtPage(userId, articleId);
    }

    /**用户_查看个人收藏文档*/
    @PostMapping("/getUserCollectArt")
    @Operation(summary = "用户_查看个人收藏文档")
    public CommonResult<List<ArticleRespVO>> getUserCollectArt(
            @RequestBody ArticlePageReqVO articlePageReqVO,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        articlePageReqVO.setUserId(userId);
        return blogUserService.getUserCollectArt(articlePageReqVO);
    }

    /**用户_取消收藏文档*/
    @GetMapping("/unCollectArt")
    @Operation(summary = "用户_取消收藏文档")
    public CommonResult<String> unCollectArt(
            @RequestParam Long articleId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.unCollectArt(userId, articleId);
    }

    /**用户_收藏文章*/
    @GetMapping("/collectArt")
    @Operation(summary = "用户_收藏文章")
    public CommonResult<String> collectArt(
            @RequestParam Long articleId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.collectArt(userId, articleId);
    }

    /**用户_点赞文章*/
    @GetMapping("/likeArt")
    @Operation(summary = "用户_点赞文章")
    public CommonResult<String> likeArt(
            @RequestParam Long articleId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.likeArt(userId, articleId);
    }

    /**用户_设置文章分类*/
    @GetMapping("/setArticleType")
    @Operation(summary = "用户_设置文章分类")
    public CommonResult<String> setArticleType(
            @RequestParam Long articleId,
            @RequestParam Long typeId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.setArticleType(userId, articleId, typeId);
    }

    /**用户_设置文章标签*/
    @PostMapping("/setArticleTag")
    @Operation(summary = "用户_设置文章标签")
    public CommonResult<String> setArticleTag(
            @RequestParam Long articleId,
            @RequestParam List<Long> selectTagId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.setArticleTag(userId, articleId, selectTagId);
    }

    /**用户_根据用户查找关注*/
    @GetMapping("/getUserFocusById")
    @Operation(summary = "用户_根据用户查找关注")
    public CommonResult<List<UsersDO>> getUserFocusById(
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.getUserFocusById(userId);
    }

    /**用户_关注用户*/
    @GetMapping("/focusUserById")
    @Operation(summary = "用户_关注用户")
    public CommonResult<String> focusUserById(
            @RequestParam Long focusId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        if(usersService.getUsers(focusId) == null){
            return CommonResult.error(400, "关注用户不存在");
        }
        if(Objects.equals(userId, focusId)){
            return CommonResult.error(400, "你不能关注自己");
        }
        UserFocusSaveReqVO userFocusSaveReqVO = new UserFocusSaveReqVO();
        userFocusSaveReqVO.setUserId(userId);
        userFocusSaveReqVO.setFocusId(focusId);
        return blogUserService.userFocus(userFocusSaveReqVO);
    }

    /**用户_取消关注用户*/
    @GetMapping("/unFocusUserById")
    @Operation(summary = "用户_取消关注用户")
    public CommonResult<String> unFocusUserById(
            @RequestParam Long focusId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.unFocusUserById(userId, focusId);
    }

    /**用户_评论文章*/
    @PostMapping("/commentArt")
    @Operation(summary = "用户_评论文章")
    public CommonResult<String> commentArt(
            @RequestBody UserCommentSaveReqVO userCommentSaveReqVO,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        userCommentSaveReqVO.setUserId(userId);
        return blogUserService.commentArt(userCommentSaveReqVO);
    }

    /**用户_回复评论*/
    @PostMapping("/replyComment")
    @Operation(summary = "用户_回复评论")
    public CommonResult<String> replyComment(
            @RequestBody UserReplySaveReqVO userReplySaveReqVO,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        userReplySaveReqVO.setUserId(userId);
        return blogUserService.replyComment(userReplySaveReqVO);
    }










    /**用户_添加好友*/
    @GetMapping("/addFriend")
    @Operation(summary = "用户_添加好友")
    public CommonResult<String> addFriend(
            @RequestParam Long friendId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }
        Long userId = jwtUtils.getIdFromToken(token);
        if(usersService.getUsers(friendId) == null){
            return CommonResult.error(400, "添加用户不存在");
        }
        if(Objects.equals(userId, friendId)){
            return CommonResult.error(400, "你不能添加自己为好友");
        }

        return blogUserService.addFriend(userId, friendId);
    }

    /**用户_查找用户好友*/
    @GetMapping("/getFriendByUser")
    @Operation(summary = "用户_查找用户好友")
    public CommonResult<List<UsersDO>> getFriendByUser(
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.getFriendByUser(userId);
    }

    /**用户_查找好友请求*/
    @GetMapping("/getFriendReq")
    @Operation(summary = "用户_查找好友请求")
    public CommonResult<List<UsersDO>> getFriendReq(
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.getFriendReq(userId);
    }

    /**用户_删除好友*/
    @GetMapping("/delFriend")
    @Operation(summary = "用户_删除好友")
    public CommonResult<String> delFriend(
            @RequestParam Long friendId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.delFriend(userId, friendId);
    }

    /**用户_同意好友请求*/
    @GetMapping("/agreeFriendRea")
    @Operation(summary = "用户_同意好友请求")
    public CommonResult<String> agreeFriendRea(
            @RequestParam Long friendId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.agreeFriendRea(userId, friendId);
    }

    /**用户_拒绝好友请求*/
    @GetMapping("/rejuctFriendRea")
    @Operation(summary = "用户_拒绝好友请求")
    public CommonResult<String> rejuctFriendRea(
            @RequestParam Long friendId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.rejuctFriendRea(userId, friendId);
    }

    /**用户_获取文章浏览历史记录*/
    @GetMapping("/browseHistory")
    @Operation(summary = "用户_获取文章浏览历史记录")
    public CommonResult<PageResult<ArticleDO>> browseHistory(
            @RequestParam Integer pageNo,
            @RequestParam Integer pageSize,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.browseHistory(pageNo, pageSize, userId);
    }

    /**用户_进入对话状态*/
    @GetMapping("/userDialogue")
    @Operation(summary = "用户_进入对话状态")
    public CommonResult<List<UserDialogueDO>> userDialogue(
            @RequestParam Long targetUserId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.userDialogue(userId, targetUserId);
    }

    /**用户_获取聊天列表*/
    @GetMapping("/getUserDialogue")
    @Operation(summary = "用户_获取聊天列表")
    public CommonResult<List<UsersDO>> getUserDialogue(
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.getUserDialogue(userId);
    }

    /**用户_获取聊天记录*/
    @GetMapping("/getDialogues")
    @Operation(summary = "用户_获取聊天记录")
    public CommonResult<List<UserDialogueDO>> getDialogues(
            @RequestParam Long groupId,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

//        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.getDialogues(groupId);
    }

    /**用户_发送消息*/
    @GetMapping("/sendMessage")
    @Operation(summary = "用户_获取聊天列表")
    public CommonResult<String> sendMessage(
            @RequestParam Long groupId,
            @RequestParam String content,
            @RequestHeader(value = "Authorization", defaultValue = "1234") String token) {
        if(!jwtUtils.validateToken(token)){
            return CommonResult.error(400, "当前未登录");
        }

        Long userId = jwtUtils.getIdFromToken(token);
        return blogUserService.sendMessage(userId, groupId, content);
    }

}










